package com.wavehi.exce.functional_interface;

import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @Author WaveHi
 * @Date 27/8/2021 上午 11:00
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Test the use of functional interface
 */
public class FunctionalInterfaceDemo {

    @Test
    public void testFunctionalInterfaceUse1() {
        //测试ToIntFunction
        ToIntFunction<Integer> integerFunction = (num) -> num * 2;

        //测试
        System.out.println(integerFunction.applyAsInt(12));
    }

    @Test
    public void testFunctionalInterfaceUse2() {
        //测试IntFunction接口
        IntFunction<Integer> intFunction = (s) -> s + 2;

    }

    @Test
    public void testFunctionalInterfaceUse3() {
        //获取函数式接口的实例
        Function<String,Integer> function = new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return s.length();
            }
        };

        //
        Stream<Integer> integerStream = Stream.of(11, 123, 4, 56, 45, 2, 3, 234, 23554, 35, 567, 532, 556, 3231, 244353 , 564, 43, 54534, 54353, 55654, 34234);
        Stream<Integer> parallel = integerStream.parallel();
        System.out.println(integerStream.findAny());
        Stream<Integer> integerStream2 = Stream.of(11, 123, 4, 56, 45, 2, 3, 234, 23554, 35, 567, 532, 556, 3231, 244353 , 564, 43, 54534, 54353, 55654, 34234);
        Stream<Integer> parallel2 = integerStream2.parallel();
        System.out.println(integerStream2.findAny());
        Stream<Integer> integerStream3 = Stream.of(11, 123, 4, 56, 45, 2, 3, 234, 23554, 35, 567, 532, 556, 3231, 244353 , 564, 43, 54534, 54353, 55654, 34234);
        Stream<Integer> parallel3 = integerStream3.parallel();
        System.out.println(integerStream3.findAny());
    }

    @Test
    public void testCollectorUse1() {
        //获取数据源
        List<Integer> integers = Arrays.asList(1, 34, 545, 64, 54);
        Stream<Integer> stream = integers.stream();
        stream.collect(toList());
        List<Integer> integers1 = Collections.unmodifiableList(integers);
        //获取收集器
        Collector<Integer, ?, Integer> integerIntegerCollector = Collectors.summingInt(Integer::intValue);

        //求和
        Stream<String> stringStream = Stream.of("Wow!", "Hello!", "Ok!");
//        stringStream.collect(Collectors.summingInt(s -> s.length()));
        ArrayList<String> collect = stringStream.collect(ArrayList<String>::new, ArrayList::add, ArrayList::addAll);
        for (String s : collect) {
            System.out.println(s);
        }

    }

    @Test
    public void testCollectorUse2() {
        //获取数据源
        Stream<String> stringStream = Stream.of("Wow!", "Hello!", "Ok!", "Yes!");

//        //获取收集器
//        List<Integer> collect = stringStream.collect(Collectors.mapping(s -> s.length(), toList()));
//        for (Integer integer : collect) {
//            System.out.println(integer);
//        }
        //分组
        Map<Integer, List<String>> collect = stringStream.collect(Collectors.groupingBy(s -> s.length()));
        collect.forEach((k,v) -> System.out.println(k + ":" + v));
    }

}
